﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class UpdateServiceDatePopup : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["DistNumber"] != null && Request.QueryString["DistNumber"].Length > 0)
                {
                    hdnItemCode.Value = Request.QueryString["ItemCode"];
                    hdnDistNumber.Value = Request.QueryString["DistNumber"];
                    LoadOSRN();
                }
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtUpdateServiceDate = (DataTable)Session["UpdateServiceDate"];
                if (dtUpdateServiceDate != null && dtUpdateServiceDate.Rows.Count > 0)
                {
                    Transaction ts = new Transaction();
                    int liSysNumber = int.Parse(string.IsNullOrEmpty(dtUpdateServiceDate.Rows[0]["AbsEntry"].ToString()) ? "0" : dtUpdateServiceDate.Rows[0]["AbsEntry"].ToString());
                    string lsRtn = ts.UpdateSerial(liSysNumber, Utils.AppConstants.ConvertToDate(txtLastServiceDate.Text));

                    if (!string.IsNullOrEmpty(lsRtn))
                    {
                        lblError.Text = GeneralFunctions.UrlFullEncode(lsRtn);
                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
                        lblError.Visible = true;
                        //Session["errorMessage"] = lsRtn;
                        //ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(lsRtn) + "','');", true);
                    }
                    else
                    {
                        lblError.ForeColor = System.Drawing.Color.Blue;
                        lblError.Text = "Operation complete sucessful!";
                        Session["UpdateServiceDate"] = null;
                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
                        lblError.Visible = true;
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('UpdateVehicleServiceDate');", true);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void LoadOSRN()
        {
            try
            {
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                string sqlCommand = "SELECT (select Name from [@MODELSALES] M where M.Code = I.U_ModelSales) AS Model, ";
                       sqlCommand += "       (select Name from [@PRODUCTSALES] P where P.Code = I.U_ProductSales and P.U_Model = I.U_ModelSales) AS Product,";
                       sqlCommand += "       (select Name from [@COLORCODE] C where C.Code = I.U_Color) AS Color,";
                       sqlCommand += "       S.DistNumber AS VinNumber, S.Notes AS EngineNumber, S.U_KeyNo AS KeyNumber, S.U_ProductionYear AS ProductionYear,";
                       sqlCommand += "       ISNULL(S.MnfDate, S.InDate) AS LastServiceDate, S.ItemCode, S.AbsEntry ";
                       sqlCommand += "  FROM OSRN S inner join OITM I on S.ItemCode = I.ItemCode ";
                       sqlCommand += " WHERE S.DistNumber = '" + hdnDistNumber.Value +"' and S.ItemCode = '" + hdnItemCode.Value + "'";
                DataTable dtOSRN = GF.GetMasterListBySqlCommand(dbType, sqlCommand);
                if (dtOSRN != null && dtOSRN.Rows.Count > 0)
                {
                    lblDocNumber.Text = dtOSRN.Rows[0]["AbsEntry"].ToString();
                    lblModelName.Text = dtOSRN.Rows[0]["Model"].ToString();
                    lblProductName.Text = dtOSRN.Rows[0]["Product"].ToString();
                    lblColorName.Text = dtOSRN.Rows[0]["Color"].ToString();
                    lblVINNumber.Text = dtOSRN.Rows[0]["VinNumber"].ToString();
                    lblEngineNumber.Text = dtOSRN.Rows[0]["EngineNumber"].ToString();
                    lblKeyNumber.Text = dtOSRN.Rows[0]["KeyNumber"].ToString();
                    lblproductionYear.Text = dtOSRN.Rows[0]["ProductionYear"].ToString();
                    txtLastServiceDate.Text = Convert.ToDateTime(dtOSRN.Rows[0]["LastServiceDate"].ToString()).ToString("dd/MM/yyyy");
                    Session["UpdateServiceDate"] = dtOSRN;
                }
               
            }
            catch (Exception ex)
            {

            }
        }
    }
}